package com.github.hiling.auth.mapper;

import com.github.hiling.auth.model.Client;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Author by hiling, Date on 12/1/2018.
 */
@Repository
public interface ClientMapper {

    @Select("""
            <script>
                select
            		`client_id` as clientId,
            		`client_name` as clientName,
            		`grant_types` as grantTypes,
            		`ip_whitelist` as ipWhitelist,
            		`scope`,
            		`status`,
            		`create_user` as createUser,
            		`create_time` as createTime,
            		`update_user` as updateUser,
            		`update_time` as updateTime,
            		`remark`
            	from oauth_client
            		<where>
            			<if test="clientId != null">
            				client_id = #{clientId}
            			</if>
            			<if test="clientSecret != null">
            				client_secret = #{clientSecret}
            			</if>
            			<if test="clientName != null">
            				client_name = #{clientName}
            			</if>
            			<if test="status != null">
            				AND status = #{status}
            			</if>
            		</where>
            </script>
            """)
    List<Client> getList(@Param("clientId") String clientId,
                         @Param("clientName") String clientName,
                         @Param("clientSecret") String clientSecret,
                         @Param("status") Integer status);

    @Select("""
            select
            		`client_id` as clientId,
            		`grant_types` as grantTypes,
            		`client_secret` as clientSecret,
            		`ip_whitelist` as ipWhitelist,
            		`scope`
            	from oauth_client
            	where client_id = #{clientId}
            		and `status` = 1
            """)
    Client getForVerify(@Param("clientId") String clientId);

    @Update("""
            update oauth_client
            set client_secret = #{newSecret},
                update_user = #{userId},
                update_time = now()
            where client_id = #{clientId}
                and client_secret = #{currentSecret}
            """)
    int refreshSecret(@Param("clientId") String clientId,
                      @Param("currentSecret") String currentSecret,
                      @Param("newSecret") String newSecret,
                      @Param("userId") String userId);

    @Update("""
            update oauth_client
            set `status` = #{status},
                update_user = #{userId},
                update_time = now()
            where client_id = #{clientId}
            """)
    int updateStatus(@Param("clientId") String clientId,
                     @Param("status") Integer status,
                     @Param("userId") String userId);

    @Insert("""
            insert into oauth_client
            (
                `client_id`,
                `client_name`,
                `client_secret` ,
                `grant_types`,
                `ip_whitelist`,
                `scope` ,
                `status`,
                `create_user`,
                `create_time`,
                `remark`
            )
            values
            (
                #{clientId},
                #{clientName},
                #{clientSecret},
                #{grantTypes},
                #{ipWhitelist},
                #{scope},
                #{status},
                #{createUser},
                now(),
                #{remark}
            )
            """)
    int insert(Client client);
}
